﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Com.Rivyer.Web.personal
{
    public partial class overview : System.Web.UI.Page
    {
        private readonly Com.Rivyer.BLL.userqueryinfoBll queryinfobll = new BLL.userqueryinfoBll();
        private readonly Com.Rivyer.BLL.userinfoBll userinfobll = new BLL.userinfoBll();
        private readonly Com.Rivyer.BLL.useroperatelogBll operatelogbll = new BLL.useroperatelogBll();
        private readonly Com.Rivyer.BLL.userloginlogBll loginlogbll = new BLL.userloginlogBll();
        protected void Page_Load(object sender, EventArgs e)
        {
            //非法操作验证
            if (Session["username"] == null
                || Session["username"].ToString().Trim().Length == 0)
            {
                Response.Write("<script>top.location='../login.aspx'</script>");
            }
            else 
            {
                //初始化userinfo, userqueryinfo
                Com.Rivyer.Model.userqueryinfoModel userqueryinfomodel = queryinfobll.getModel(Session["username"].ToString());
                username.Text = userqueryinfomodel.username;
                createdate.Text = userqueryinfomodel.createdate.ToString();
                logincount.Text = userqueryinfomodel.logincount.ToString();
                if (int.Parse(logincount.Text) == 1)
                {
                    latestlogin.Text = "没有记录!";
                }
                else
                {
                    latestlogin.Text = userqueryinfomodel.latestlogin.ToString();
                }
                latestoperate.Text = userqueryinfomodel.latestoperate;

                if (!IsPostBack)
                {
                    Com.Rivyer.Model.userinfoModel userinfomodel = userinfobll.GetModel(username.Text);
                    realname.Text = userinfomodel.realname.ToString();
                    age.Text = userinfomodel.age.ToString();
                    homeadress.Text = userinfomodel.homeadress;
                    telephone.Text = userinfomodel.telephone;
                    qqnum.Text = userinfomodel.qq;
                }
            }
        }   

        protected void btn_edit_Click(object sender, EventArgs e)
        {
                //点击保存
                Com.Rivyer.Model.userinfoModel infomodel = new Model.userinfoModel();
                infomodel.age = int.Parse(age.Text.Trim());
                infomodel.homeadress = homeadress.Text.Trim();
                infomodel.qq = qqnum.Text.Trim();
                infomodel.realname = realname.Text.Trim();
                infomodel.telephone = telephone.Text.Trim();
                infomodel.username = Session["username"].ToString();
                if (!userinfobll.Update(infomodel))
                {
                    Maticsoft.Common.MessageBox.Show(this, "更新失败!");
                }
                else
                {
                    Model.useroperatelogModel logmodel = new Model.useroperatelogModel();
                    logmodel.createdate = DateTime.Now;
                    logmodel.operation = "更新";
                    logmodel.username = Session["username"].ToString();
                    logmodel.description = string.Format("个人用户信息");
                    operatelogbll.Add(logmodel);
                    latestlogingrid.DataBind();
                    Maticsoft.Common.MessageBox.Show(this, "更新成功!");
                }
            
        }
    }
}